<!doctype html>

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<?php
include_once('navbar.php');
include_once('superheader.php');
?>

<script>
$(function() {
	
	$( "#subset" ).buttonset();
	$( "#button5" ).button();
	$( "#button4" ).button();
	$( "#button3" ).button();
	
	$("#response").html("");
        


          var params =  {
          colReq2: "users",
        };

        var data = JSON.stringify(params);

        $.ajax({
                    cache: false,
                    type: "json",
                    url: "/phpfunctions/lists/listSoftware.php",
                    contentType: "application/json",
                    processData: true,
                    data: data,
                    success: function(res, textStatus, jqXHR){
		      var container = $("#response");
			 var table = $("<table border=\"1\">");
			 var head = $("<tr>");
			 //head.append("<th>MongoId</th>");
			 head.append("<th>User Name</th>");
			 head.append("<th>Password</th>");
			 head.append("<th>Role</th>");
		 	 table.append(head);

			 for(var key in res.data.messages)
			 {
				var tr = $("<tr>");
				var message = res.data.messages[key];
				var td = $("<td>");
				/*td.html(message['_id']);
				tr.append(td);*/
					
				var td = $("<td>");
				td.html(message.user);
				tr.append(td);
					
				var td = $("<td>");
				td.html(message.password);
				tr.append(td);
					
				var td = $("<td>");
				td.html(message.role);
				tr.append(td);
					
				table.append(tr);			
			 }
                         //container.append(JSON.stringify(res['data']));
			container.html(table);
                    },
                    complete: function(XMLHttpRequest, text_status) {

                    }
                  });
                  

	
	
});
</script>

</head>


<body>
	
	<h2 style="position: relative; top:50px; left:400px; height:10px">Users:</h2>

	<div id="response" style="position: relative; top:50px; left:400px; height:0px">
      Stuff to be replaced
    </div>
	
	<?php if($_SESSION['role'] === "admin" || $_SESSION['role'] === "support"||$_SESSION['role'] === "faculty") : ?>
	<h2>Add User:</h2>
	<?php if(isset($_GET["error"])) : ?>
<?php if($_GET["error"] == 'emptyUser') : ?>
    <div class="ui-widget">
	<p>Please enter a valid username and password.</p>
	</div>
	<?php endif; ?>
	<?php if($_GET["error"] == 'duplicateUser') : ?>
    <div class="ui-widget">
	<p>That user already exists.</p>
	</div>
	<?php endif; ?>
<?php endif; ?>
	<form action="phpfunctions/adduser.php" method="post">
 	<p>Username: <input type="text" name="username"><br><p>
 	<p>Password: <input type="text" name="password"><p>
 	<?php if($_SESSION['role'] === "admin") : ?>
 	<input type="radio" name="role" value="admin">Admin<br>
 	<?php endif; ?>
 	<?php if($_SESSION['role'] === "admin" || $_SESSION['role'] === "support") : ?>
 	<input type="radio" name="role" value="support">Support<br>
 	<input type="radio" name="role" value="faculty">Faculty<br>
 	<?php endif; ?>
 	<input type="radio" name="role" value="user">User<br>
 	
 	
 	<button id="button4" input type="submit" name="submit" value="Submit me!" />Submit</button>
	</form>
	<?php endif; ?>
	
	
	<?php if($_SESSION['role'] === "admin" || $_SESSION['role'] === "support") : ?>
		
		
	<h2>Remove User:</h2>
	<?php if(isset($_GET["error"])) : ?>
		<?php if($_GET["error"] == 'deleteErrorUser') : ?>
    	<div class="ui-widget">
		<p>No user with that username was found.</p>
		</div>
		<?php endif; ?>
	<?php endif; ?>
	<form action="phpfunctions/deleteuser.php" method="post">
	<select name="username">
		<?php
	        $z = new Mongo();
	        $db = $z->EMU;
	        $collection = $db->users;
			$newCursor = $collection->find();
	
			foreach ($newCursor as $item) 
			{
				print "<option>";
				print $item['user'];
				print "</option>";
			}
		?>
	</select></p>
 	<button id="button5" input type="submit" name="submit" value="Submit me!" />Submit</button>
	</form>

	
<br>
    <?php if($_SESSION['role'] === "admin") : ?>
    <h2>DB Backups:</h2>
<form>
<div id="button3">
<a href="backuppage.php"><id="nav1" name="radio"><label for="nav1">Monitor or Create Backup</label></a>
		<br/>
	</div>
</form>

    <?php endif; ?>
    <?php endif; ?>
    

</body>


</html>